package ar.edu.unlu.sistemas.ws.test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import ar.edu.unlu.sistemas.ws.business.Message;

public class TCPTester {

	private static String query = "debian linux";
	private static String IP = "192.168.1.4";
	private static int PORT = 3600;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		Socket s = new Socket();
		if (false){
			try {
				System.out.println(new Date()+" Inicio conexion para propagacion de query\""+query+"\"");
				InetSocketAddress address = new InetSocketAddress(IP,PORT);
				s.connect(address);
				s.setTcpNoDelay(true);
				s.setPerformancePreferences(2, 1, 0);
				System.out.println(new Date()+" Se establecio conexion para propagacion de query\""+query+"\"");
				DataOutputStream out = new DataOutputStream(s.getOutputStream());
				System.out.println(new Date()+" Tengo ouput de conexion para propagacion de query\""+query+"\"");
				System.out.println(new Date()+" Preparo query\""+query+"\" a enviar");
				out.writeUTF(query);
				System.out.println(new Date()+" Envio query\""+query+"\"");
				try {
					DataInputStream in = new DataInputStream(s.getInputStream());
					System.out.println(new Date()+" Tengo input de conexion para propagacion de query\""+query+"\"");
					System.out.println(new Date()+" Respuesta: "+in.readUTF());
				} catch (Exception cne) {
					System.out.println(new Date()+" Ocurrio esperando la respuesta del movil para la query \""+query+"\"");
				}
				System.out.println(new Date()+" Cierro conexion de propagacion de query\""+query+"\"");
				s.close();
			} catch (UnknownHostException uhe) {
				System.out.println(new Date()+" Ocurrio un error estableciendo conexión ");
			} catch (IOException ioe) {
				System.out.println(new Date()+" Ocurrio un erro I/O ");
			}
		} else {
			try {
				System.out.println(new Date()+" Inicio conexion para propagacion de query\""+query+"\"");
				InetSocketAddress address = new InetSocketAddress(IP,PORT);
				s.connect(address);
				s.setSendBufferSize(1024);
				System.out.println(new Date()+" Se establecio conexion para propagacion de query\""+query+"\"");
				ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
				System.out.println(new Date()+" Tengo ouput de conexion para propagacion de query\""+query+"\"");
				Message msg = new Message();
				msg.setType("PRUEBA");
				msg.setQuery(query);
				System.out.println(new Date()+" Preparo query\""+query+"\" a enviar");
				out.writeObject(msg);
				System.out.println(new Date()+" Envio query\""+query+"\"");
				try {
					ObjectInputStream in = new ObjectInputStream(s.getInputStream());
					System.out.println(new Date()+" Tengo input de conexion para propagacion de query\""+query+"\"");
					Message response = (Message) in.readObject();
					System.out.println(response.getType());
				} catch (ClassNotFoundException cne) {
					System.out.println(new Date()+" Ocurrio esperando la respuesta del movil para la query \""+query+"\"");
				}
				System.out.println(new Date()+" Cierro conexion de propagacion de query\""+query+"\"");
				s.close();
			} catch (UnknownHostException uhe) {
				System.out.println(new Date()+" Ocurrio un error estableciendo conexión ");
			} catch (IOException ioe) {
				System.out.println(new Date()+" Ocurrio un erro I/O ");
			}
		}
		

	}

}
